package cn.edu.mju.property.dao.impl;

import cn.edu.mju.property.dao.BuildingDao;
import cn.edu.mju.property.dao.UnitDao;
import cn.edu.mju.property.entity.Building;
import cn.edu.mju.property.entity.Unit;
import cn.edu.mju.property.utils.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class BuildingDaoImpl implements BuildingDao {

    @Override
    public boolean addBuilding(Building building) {
        return false;
    }

    @Override
    public boolean delBuilding(int id) {
        return false;
    }

    @Override
    public boolean updateBuilding(Building building) {
        return false;
    }

    @Override
    public List<Building> findBuildingByPage(int start, int rows) {
        Building building = null;
        List<Building> Buildinglist = new ArrayList<Building>();
        try {
            Connection conn =  JDBCUtil.getConnect();
            String sql = "select * from building limit ? , ? ";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,start);
            pstmt.setInt(2,rows);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()){
                building = new Building();
                building.setId(rs.getInt("id"));
                building.setName(rs.getString("name"));
                building.setType(rs.getString("type"));
                Buildinglist.add(building);
            }
            pstmt.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Buildinglist;
    }

    @Override
    public int findTotalCount() {
        try {
            Connection conn =  JDBCUtil.getConnect();
            String sql = "select count(*) from building";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            rs.next();
            int num =  rs.getInt("count(*)");
            pstmt.close();
            conn.close();
            return num;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
}
